package com.wzdigit.wms.wms.service.core;

import com.wzdigit.mes.basic.api.BasicDataItemDetailApi;
import com.wzdigit.mes.basic.dto.DataItemDetailDTO;
import com.wzdigit.wms.basic.domain.dtos.oqc.OqcInspDtlDto;
import com.wzdigit.wms.basic.domain.inventory.GoodsVersion;
import com.wzdigit.wms.basic.domain.oqc.OqcInsp;
import com.wzdigit.wms.basic.enums.QaStatusEnum;
import com.wzdigit.wms.basic.enums.QmsOqcStatusEnum;
import com.wzdigit.wms.basic.mapper.core.oqc.OqcInspMapper;
import com.wzdigit.wms.wms.service.task.AutoQmsGdnService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 
 * 翻拍-判检执行【判检单主档处理】
 * @author Administrator
 *
 */
@Service
@Slf4j
public class OqcHeaderNewTaskService {

	@Autowired
    private OqcInspMapper oqcInspMapper;
	
	@Autowired
    private OqcGroupNewTaskService oqcGroupNewTaskService;

	@Autowired
	private AutoQmsGdnService autoQmsGdnService;

	@Reference(version = "${dubbo.consumers.basic.data.item.detail.api:1.0.0}", check = false, timeout = 300000)
	private BasicDataItemDetailApi dataItemDetailApi;
	
	/**
	 * 判检单主档执行入口
	 * 
	 * @param oqcInspId				送检单主档ID
	 * @param oqcInspDtlList		送检单明细集合
	 * @param isCallSAPInterface	是否回传SAP标识
	 * @throws Exception
	 */
	public void execute(Integer oqcInspId, List<OqcInspDtlDto> oqcInspDtlList, boolean isCallSAPInterface) throws Exception {
		OqcInsp oqcInsp = oqcInspMapper.selectById(oqcInspId);

		boolean aotoGdnFlag=false;
		DataItemDetailDTO dto = dataItemDetailApi.getDataItemDetailByPCodeAndName("oqc_gdn_flag", "key");
		if (dto!=null){
			if (!"0".equals(dto.getItemValue())){
				aotoGdnFlag=true;
			}
		}
    	
    	// 判断结果
    	if(oqcInsp.getInspResult().equals(QmsOqcStatusEnum.JUDGE_RESULT_SUCCESS.getCode())) {
    		// 判过 = 合格
    		oqcInsp.setInspResult(QaStatusEnum.OK.code);
    	} else if (oqcInsp.getInspResult().equals(QmsOqcStatusEnum.JUDGE_RESULT_FAIL.getCode())) {
    		// 判退 = 不合格
    		oqcInsp.setInspResult(QaStatusEnum.NG.code);
    	}
    	
    	// 当前主档的判检成功的数量
    	int headerDealOkSize = 0;
    	
    	// 根据【工厂、仓库】分组
        Map<String, List<OqcInspDtlDto>> oqcInspGroup = oqcInspDtlList.stream().collect(Collectors.groupingBy(g -> (g.getFactory() + "#" + g.getWhsCode())));

		List<GoodsVersion> goodsVersionList =new ArrayList<>();
		// 遍历送检单分组start
        for (String oqcInspGroupKey : oqcInspGroup.keySet()) {
        	try {
        		oqcInspDtlList = oqcInspGroup.get(oqcInspGroupKey);
				goodsVersionList=oqcGroupNewTaskService.execute(oqcInsp, oqcInspDtlList, isCallSAPInterface);
				headerDealOkSize +=goodsVersionList.size() ;
        		log.info("【定时任务-产品判检】主档分组执行成功：{}：{}，数量：{}", oqcInspId, oqcInspGroupKey, oqcInspDtlList.size());
        	} catch (Exception e) {
				log.error("【定时任务-产品判检】主档分组执行异常/失败：{}：{}，数量：{}", oqcInspId, oqcInspGroupKey, oqcInspDtlList.size());
				e.printStackTrace();

        	}
        	if (aotoGdnFlag){
				//自动出库
				try {
					if (goodsVersionList.size()>0){
						autoQmsGdnService.autoGdn(goodsVersionList,oqcInsp);
					}
				}catch (Exception e){
					log.error("自动出库失败");
					e.printStackTrace();
				}
			}
		}// 遍历送检单分组完毕end
        
        if(headerDealOkSize == 0) {
        	throw new Exception("当前主档判检成功的数量为0");
        }
    }
}
